package com.example.service.impl;

import com.example.domain.po.Course;
import com.example.domain.po.Orders;
import com.example.domain.po.User;
import com.example.domain.vo.UserVo;
import com.example.mapper.UserMapper;
import com.example.service.CourseService;
import com.example.service.OrdersService;
import com.example.service.UserService;
import com.example.util.PageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    CourseService courseService;

    @Autowired
    OrdersService ordersService;


    @Override
    public List<User> findByDepartmentId(Integer departmentId) {
        return userMapper.findByDepartmentId(departmentId);
    }

    @Override
    public PageBean<UserVo> list(Integer pageNum, Integer pageSize, Integer hospitalId, Integer departmentId, String name) {
        PageBean<UserVo> pb = new PageBean<>();
        PageHelper.startPage(pageNum, pageSize);

        List<UserVo> dockerList = userMapper.list(hospitalId, departmentId, name, 2);
        Page<UserVo> p = (Page<UserVo>) dockerList;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return pb;
    }

    @Override
    public void add(User user) {
        //医生
        if (user.getPhone() != null) {
            user.setRole(null);
        } else {
            user.setRole(2);
        }
        userMapper.add(user);
    }

    @Override
    public void del(Integer id) {
        userMapper.del(id);
    }

    @Override
    public void update(User user) {

        userMapper.update(user);
    }

    @Override
    public User findByPhone(String phone) {
        return userMapper.findByPhone(phone);
    }


    @Override
    public User userInfo(Integer userId) {
        return userMapper.userInfo(userId);
    }

    @Override
    public void setStudent(Integer id) {
        User user = new User();
        user.setId(id);
        user.setRole(1);
        userMapper.update(user);
    }

    @Override
    public void setDocker(Integer id) {
        User user = new User();
        user.setId(id);
        user.setRole(2);
        userMapper.update(user);
    }

    @Override
    public List<Map<String, Object>> listByLike(String name) {
        return userMapper.listByLike(name);
    }

    @Override
    public void register(String phone) {
        userMapper.register(phone);
    }

    @Override
    public Map<String, Object> getBylike(String phone) {
        return userMapper.getBylike(phone);
    }


    @Override
    public String payCourse(Long orderNo){
        //获取订单
        Orders orders = ordersService.getByOrderNo(orderNo);
        //获取课程对象
        Course course = courseService.findById(orders.getCourseId());
        //获取用户对象
        User user = userMapper.getById(orders.getUserId());
        //获取医生对象
        User doctor = userMapper.getById(course.getDockerId());

        //判断用户余额是否大于等于课程价格
        if(user.getBalance().compareTo(course.getPrice())>=0){
            //用户扣款
            user.setBalance(user.getBalance().subtract(course.getPrice()));
            userMapper.update(user);
            //医生收益增加
            doctor.setBalance(doctor.getBalance().add(course.getPrice()));
            userMapper.update(doctor);
            //订单修改为已支付
            orders.setStatus(2);
            ordersService.updateById(orders);
            //课程购买量加1
            course.setCount(course.getCount()+1);
            courseService.update2(course);
            return "恭喜您，购买成功";
        }else{
            return "余额不足，请充值";
        }
    }

    @Override
    public PageBean<User> page(Integer role, String name, String phone, Integer pageNum, Integer pageSize) {
        PageBean<User> pb = new PageBean<>();
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userMapper.page(role, name, phone);

        list.forEach(item -> {
            if (item.getRole().equals(1)) item.setRoleF("普通用户");
            if (item.getRole().equals(2)) item.setRoleF("医生");
        });

        Page<User> p = (Page<User>) list;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return pb;

    }

    @Override
    public void addAccount(User user) {

        if (user.getId() == null) {
            user.setRole(1);
            userMapper.addAccount(user);
        } else {
            userMapper.updateAccount(user);
        }

    }

    @Override
    public void editInfo(User user) {
        userMapper.editInfo(user);
    }

    @Override
    public User getByPhone(String phone) {
        return userMapper.findByPhone(phone);
    }


}
